package com.tc.xushu;

import javax.persistence.EntityManager;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.TypeVariable;

/**
 * @Auther: tianchao
 * @Date: 2021/12/5 21:01
 * @Description:
 */
public class MyJpaRepository implements InvocationHandler {
    EntityManager em;

    Class pojoClass;

    public MyJpaRepository(EntityManager em, Class pojoClass) {
        this.em = em;
        this.pojoClass = pojoClass;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        MyJpaProxy myJpaRepository = new MyJpaProxy(em, pojoClass);
        String methodName = method.getName();
        Class<?>[] parameterTypes = method.getParameterTypes();
        Method proxyMethod = myJpaRepository.getClass().getMethod(methodName, parameterTypes);
        System.out.println("查询");
        return proxyMethod.invoke(myJpaRepository, args);
    }
}
